let requestUrl = null;
let directionArray = ["中间", "左边", "右边"];

function addHandler() {
	let schoolId = $("#school_id").combobox("getValue");
	let type = $("#_type_").combobox("getValue");

	if (schoolId) {
		$("#schoolId").combobox("setValue", schoolId);
	}
	if (type) {
		$("#type").combobox("setValue", type);
	}

	requestUrl = "/jingmai_category/insert";
	
	$("#jingmai_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#jingmai_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/jingmai_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#seq").numberspinner("setValue", rowData.seq);

		// 刷新下拉框数据
		$("#prevId").combobox("reload", {
			schoolId: rowData.schoolId
		}).combobox("setValue", rowData.prevId);

		$("#nextId").combobox("reload", {
			schoolId: rowData.schoolId
		}).combobox("setValue", rowData.nextId);
		
		$("#schoolId").combobox("setValue", rowData.schoolId);
		$("#direction").combobox("setValue", rowData.direction);
		$("#inlayEffect").textbox("setValue", rowData.inlayEffect);
		$("#activeEffect").textbox("setValue", rowData.activeEffect);

		$("#jingmai_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#jingmai_list").datagrid("getSelected");

	if(rowData) {
		$("#jingmaiId").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#jingmai_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/jingmai_category/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#jingmai_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！");
	}
}

$(document).ready(function() {

	$("#school_id").combobox({
		url: "/school/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "--选择门派--",
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#_type_").combobox({
		url: "/jingmai_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "--选择类型--"
	});

	$("#_name_").textbox({
		width: 150,
		prompt: "输入经脉名称"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let name = $("#_name_").textbox("getValue");
		let type = $("#_type_").combobox("getValue");
		let schoolId = $("#school_id").combobox("getValue");

		$("#jingmai_list").datagrid("reload", {
			schoolId: schoolId,
			type: type,
			name: name
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 120,
		required: true
	});

	$("#type").combobox({
		url: "/jingmai_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true
	});

	fetchSelect("#direction", directionArray, {
		width: 120,
		required: true
	});

	$("#seq").numberspinner({
		min: 1,
		max: 7,
		width: 120,
		required: true
	});

	$("#prevId").combobox({
		url: "/jingmai_category/selectBySchoolId",
		valueField: "id",
		textField: "name",
		width: 120,
		required: false,
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#nextId").combobox({
		url: "/jingmai_category/selectBySchoolId",
		valueField: "id",
		textField: "name",
		width: 120,
		required: false,
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#schoolId").combobox({
		url: "/school/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		},
		onSelect: function (record) {
			$("#prevId").combobox("reload", {
				schoolId: record.id
			})
		}
	});

	$("#activeEffect").textbox({
		width: 525,
		height: 100,
		required: true,
		multiline: true
	});

	$("#inlayEffect").textbox({
		width: 525,
		height: 100,
		required: false,
		multiline: true
	});
	
	$("#jingmai_dialog").dialog({
		title: " ",
		width: 640,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#jingmai_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);

						$(selector).form("clear");
						$("#jingmai_dialog").dialog("close");
						$("#jingmai_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#jingmai_dialog").dialog("close");
				$("#jingmai_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#jingmai_list", "/jingmai_category/upload");

	fetchUploadDialog("/jingmai_category/updateImageById", function () {
		$("#jingmai_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#jingmai_list").datagrid({
		url: "/jingmai_category/selectByPage",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "name", title: "经脉名称", align: "center", width: 80},
			{field: "image", title: "图片", align: "center", width: 80
				, formatter: function(value) {
					if (value) {
						return "<img height='" + size + "' src='" + value + "' />";
					} else {
						return "/";
					}
				}
			},
			{field: "type", title: "经脉类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/jingmai_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "seq", title: "序号", align: "center", width: 80},
			{field: "direction", title: "方向", align: "center", width: 80
				, formatter: function(value) {
					return "<div>" + directionArray[value] + "</div>";
				}
			},
			{field: "prevId", title: "前置经脉", align: "center", width: 80
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/jingmai_category/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#prev_" + rowIndex).attr({
								"title": data.name,
								"src": data.image,
								"alt": data.name
							});
						}, error);

						return "<img height='" + size + "' id='prev_" + rowIndex + "' />";
					}

					return "/";
				}
			},
			{field: "nextId", title: "后置经脉", align: "center", width: 80
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/jingmai_category/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#next_" + rowIndex).attr({
								"title": data.name,
								"src": data.image,
								"alt": data.name
							});
						}, error);

						return "<img height='" + size + "' id='next_" + rowIndex + "' />";
					}

					return "/";
				}
			},
			{field: "activeEffect", title: "激活效果", align: "center", width: 200
				, formatter: function (value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "inlayEffect", title: "镶嵌效果", align: "center", width: 200
				, formatter: function (value) {
					if (value) {
						return "<div class='ell' title='" + value + "'>" + value + "</div>";
					}

					return "/";
				}
			},
			{field: "schoolId", title: "门派", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/school/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#school_" + rowIndex).attr({
							"src": data.image,
							"title": data.name
						});
					}, error);

					return "<img height='" + size + "' id='school_" + rowIndex + "' />";
				}
			}
		]]
	});

});